package com.yds.bluetooth

import android.annotation.SuppressLint
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCharacteristic
import android.util.Log

class BLEDataReader {
    companion object {
        private const val TAG = "YDSimon"
    }

    private var mGatt: BluetoothGatt? = null

    fun readFromAllReadableCharacteristics(gatt: BluetoothGatt?) {
        mGatt = gatt
        val services = mGatt!!.services
        for (service in services) {
            val characteristics = service.characteristics
            for (characteristic in characteristics) {
                if (characteristic.properties and BluetoothGattCharacteristic.PROPERTY_READ != 0) {
                    tryToReadData(characteristic)
                }
            }
        }
    }

    private fun tryToReadData(characteristic: BluetoothGattCharacteristic) {
        @SuppressLint("MissingPermission") val success = mGatt!!.readCharacteristic(characteristic)
        if (success) {
            Log.i(TAG, "Reading data from characteristic: " + characteristic.uuid)
        } else {
            Log.e(TAG, "Failed to read data from characteristic: " + characteristic.uuid)
        }
    }


}